package com.atguigu.yygh.hosp.common.base;

import com.atguigu.yygh.hosp.common.result.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;

/**
 * @version 1.0
 * @Author: 许啸林 控制层工具类
 * @Date: 2021/6/30 13:34
 */

public class BaseController {
    private final static String PAGE_CURRENT = "pageNum";
    private final static String PAGE_SIZE = "pageSize";

    /**
     * 获取分页对象
     * @return
     */
    public static Page getPage() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
                .getRequestAttributes()).getRequest();
        String currentStr = request.getParameter(PAGE_CURRENT);
        String limitStr = request.getParameter(PAGE_SIZE);
        if(currentStr != null && limitStr != null ) {
           int current = Integer.parseInt(currentStr);
           int size = Integer.parseInt(limitStr);
           Page page = new Page();
           page.setCurrent(current);
           page.setSize(size);
            return page;
        }else {
            throw new RuntimeException("分页数据出错");
        }

    }

    /**
     * boolean类型统一返回
     * @param flag
     * @return
     */
    public static Result getResult(boolean flag) {
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

}
